<?php

/**
 * 管理者接口
 * @author: 喵了个咪  <wenzhenxi@vip.qq.com>    2015-9-16
 */
class Api_Manage extends PhalApi_Api {

    public function getRules() {

        return array(
            //登录
            'login'           => array(
                'userName' => array('name' => 'userName', 'require' => true, 'min' => 2, 'max' => 55,'desc' => '用户登录名称'),
                'passWord' => array('name' => 'passWord', 'require' => true, 'min' => 32, 'max' => 32 ,'desc' => '用户密码MD5值'),
            ),
            //修改密码
            'setPassWord'     => array(
                'uId'   => array('name' => 'uId', 'require' => true),
                'oldPw' => array('name' => 'oldPw', 'require' => true, 'min' => 32, 'max' => 32),
                'newPw' => array('name' => 'newPw', 'require' => true, 'min' => 32, 'max' => 32),
            ),
            //修改信息
            'setInfo'         => array(
                'uId'   => array('name' => 'uId', 'require' => true),
                'name'  => array('name' => 'name', 'require' => true, 'min' => 2),
                'phone' => array('name' => 'phone', 'require' => true, 'min' => 11, 'max' => 11),
            ),
            //帐户列表
            'getManagList'    => array(
                'uId' => array('name' => 'uId', 'require' => true),
            ),
            //新增账户
            'setManag'        => array(
                'uId'      => array('name' => 'uId', 'require' => true),
                'userName' => array('name' => 'userName', 'require' => true, 'min' => 2, 'max' => 55),
                'passWord' => array('name' => 'passWord', 'require' => true, 'min' => 32, 'max' => 32),
                'name'     => array('name' => 'name', 'require' => true, 'min' => 2),
                'rId'      => array('name' => 'rId', 'require' => true, 'min' => 1),
                'phone'    => array('name' => 'phone', 'require' => true, 'min' => 11, 'max' => 11),
            ),
            //编辑账户
            'updateManag'     => array(
                'uId'      => array('name' => 'uId', 'require' => true),
                'adminId'  => array('name' => 'adminId', 'require' => true),
                'passWord' => array('name' => 'passWord', 'require' => true, 'min' => 32, 'max' => 32),
                'name'     => array('name' => 'name', 'require' => true, 'min' => 2),
                'phone'    => array('name' => 'phone', 'require' => true, 'min' => 11, 'max' => 11),
                'roleId'   => array('name' => 'roleId', 'require' => true),
            ),
            //停用/启用帐户
            'updateManagType' => array(
                'uId'     => array('name' => 'uId', 'require' => true),
                'adminId' => array('name' => 'adminId', 'require' => true),
                'type'    => array('name' => 'type', 'require' => true),
            ),
        );
    }

    /**
     * 账户设置
     *
     * @return int code 默认返回200成功
     */
    public function setInfo() {

        //验证管理员账号
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->checkadminId($this->uId);
        //修改信息
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->setInfo($this->uId, $this->name, $this->phone);
    }

    /**
     * 修改密码
     *
     * @return int code 默认返回200成功
     */
    public function setPassWord() {

        //验证管理员账号
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->checkadminId($this->uId);
        //验证密码
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->validationPassWord($this->oldPw, $this->uId);
        //修改密码
        $Domain_Manage->setPassWord($this->newPw, $this->uId);
    }

    /**
     * 登录
     *
     * @desc 用户用户登录 返回用户可用菜单 以及可以使用的API
     * @return int uId 管理员用户ID
     * @return string name 管理员用户名称
     * @return string phone 管理员用户手机号码
     * @return array mList 菜单列表(包含mId(菜单ID),icon(菜单图标),url(菜单地址),name(菜单名称),projectName(项目名称),pId(项目ID))
     * @return array apiList 用户可使用Api列表(可以工具列表来对应按钮是否可以点击)
     */
    public function login() {

        //验证管理员用户获取ID
        $Domain_Managers = new Domain_Manage();
        $adminId         = $Domain_Managers->login($this->userName, $this->passWord);
        $data            = array();
        $data['uId']     = $adminId['adminId'];
        $data['name']    = $adminId['name'];
        $data['phone']   = $adminId['phone'];
        //通过用户ID获取菜单和apiId列表
        list($mList, $apiList) = $Domain_Managers->getadminIdBymListapiList($adminId['adminId']);
        $data['mList']   = $mList;
        $data['apiList'] = $apiList;
        DI()->response->setMsg('登录成功');

        return $data;
    }

    /**
     * 帐户列表
     */
    public function getManagList() {

        //验证管理员账号
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->checkadminId($this->uId);
        //权限验证
        $Domain_Validation = new Domain_Validation();
        $Domain_Validation->validation('manag.getManagList', $this->uId);
        //获取账户列表
        $Domain_Manage = new Domain_Manage();
        return $Domain_Manage->getManagList();
    }

    /**
     * 新增账户
     */
    public function setManag() {

        //验证管理员账号
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->checkadminId($this->uId);
        //权限验证
        $Domain_Validation = new Domain_Validation();
        $Domain_Validation->validation('manage.setManag', $this->uId);
        //验证用户名是否存在
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->increaseValidation($this->userName);
        //创建用户
        $adminId = $Domain_Manage->increaseManage($this->userName, $this->passWord, $this->name, $this->phone);
        //创建分配角色
        $Domain_Role = new Domain_Role();
        $Domain_Role->setAdminIdRId($adminId, $this->rId);
    }

    /**
     * 编辑账户
     */
    public function updateManag() {

        //验证管理员账号
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->checkadminId($this->uId);
        //权限验证
        $Domain_Validation = new Domain_Validation();
        $Domain_Validation->validation('manage.updateManag', $this->uId);
        if (!is_array($this->roleId)) {
            throw new PhalApi_Exception_BadRequest(T('roleId No array'));
        } else {
            $this->roleId = array_unique($this->roleId);
        }
        $Domain_Manage = new Domain_Manage();

        //修改用户
        $Domain_Manage->updateManage($this->adminId, $this->passWord, $this->name, $this->phone);
        //更新用户角色绑定
        $Domain_Role = new Domain_Role();
        $Domain_Role->setAdminIdRIdList($this->adminId, $this->roleId);
    }

    /**
     * 停用/启用帐户
     */
    public function updateManagType() {

        //验证管理员账号
        $Domain_Manage = new Domain_Manage();
        $Domain_Manage->checkadminId($this->uId);
        //权限验证
        $Domain_Validation = new Domain_Validation();
        $Domain_Validation->validation('manage.updateManagType', $this->uId);

        $Domain_Manage = new Domain_Manage();
        //修改用户
        $Domain_Manage->updateManageType($this->adminId, $this->type);
    }
}


